package com.ss.android.f.b.a.a;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;

import com.bytedance.common.utility.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.Cipher;

public final class DirectoryCacheHelper extends c {
    static final class a implements Runnable {
        private final DirectoryCacheHelper a;

        private a(DirectoryCacheHelper arg1) {
            this.a = arg1;

        }

        a(DirectoryCacheHelper arg1, byte arg2) {
            this(arg1);
        }

        public final void run() {
            this.a.a();
            this.a.a = true;
        }
    }

    volatile boolean a;
    private final String b;
    private final Context c;
    private String e;
    private volatile JSONObject f;
    private boolean g;
    private static volatile Pair h;
    private static volatile Pair i;

    public DirectoryCacheHelper(Context arg7, String arg8) {
        int v5 = 6;
        this.e = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.ss.android.article.news/bytedance";
        this.g = false;
        this.a = false;
        this.b = Environment.getExternalStorageState();
        this.e = arg8;
        this.c = arg7;
        String[] v2 = new String[v5];
        v2[0] = "openudid";
        v2[1] = "clientudid";
        v2[2] = "serial_number";
        v2[3] = "sim_serial_number";
        v2[4] = "udid";
        v2[5] = "device_id";
        int v1;
        for (v1 = 0; v1 < v5; ++v1) {
            String v0 = v2[v1];
            try {
                if (this.e == null) {
                    continue;
                }

                File v3 = new File(this.e + File.separator + v0 + ".dat");
                if (!v3.exists()) {
                    continue;
                }

                v3.delete();
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    private String a(byte[] arg7) {
        String v0_2;
        Object v0 = null;
        if (DirectoryCacheHelper.h != null && (Arrays.equals(arg7, (byte[]) DirectoryCacheHelper.h.first))) {
            v0 = DirectoryCacheHelper.h.second;
            return ((String) v0);
        }

        if (DirectoryCacheHelper.i != null && (Arrays.equals(arg7, (byte[]) DirectoryCacheHelper.i.second))) {
            v0 = DirectoryCacheHelper.i.first;
            return ((String) v0);
        }

        String v1 = null;
        try {
            v0_2 = this.c();
            byte[] v2 = Base64.decode(arg7, 0);
            SecureRandom v3 = new SecureRandom();
            Key v0_3 = android.support.design.a.a(v0_2.getBytes());
            Cipher v4 = Cipher.getInstance("DES");
            v4.init(2, v0_3, v3);
            v0_2 = new String(v4.doFinal(v2), "UTF-8");
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0_2 = v1;
        }

        if (v0_2 != null) {
            DirectoryCacheHelper.h = new Pair(arg7, v0_2);
        }

        label_8:
        return ((String) v0);
    }

    protected final String a(String arg5) {
        String v0 = this.c(arg5);
        if (Logger.debug()) {
            Logger.d("DirectoryCacheHelper", "getCachedString key = " + arg5 + " value = " + v0 + " cacheDir = " + this.e);
        }

        return v0;
    }

    final void a() {
        RandomAccessFile v6;
        FileLock v1_1 = null;
        FileLock v2_1;
        RandomAccessFile v2 = null;
        RandomAccessFile v1 = null;
        if (!"mounted".equals(this.b)) {
            return;
        }

        try {
            File v0_2 = new File(this.e);
            if (!v0_2.exists() && !v0_2.mkdirs()) {
                return;
            }

            v2 = new RandomAccessFile(new File(this.b()), "rwd");
            v1_1 = v2.getChannel().lock();
            byte[] v0_3 = this.d(this.f.toString());
            v2.setLength(0);
            v2.write(v0_3);
        } catch (Exception v0_1) {
            Logger.d("DirectoryCacheHelper", "load openudid exception " + v0_1);
        } finally {
            if (v1_1 != null) {
                try {
                    v1_1.release();
                } catch (Exception e) {

                }
            }

            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
        }
    }

    protected final void a(String arg5, String arg6) {
        String v3 = null;
        if (Logger.debug()) {
            Logger.d("DirectoryCacheHelper", "cacheString key = " + arg5 + " value = " + arg6 + " cacheDir = " + this.e);
        }

        if (!TextUtils.isEmpty(((CharSequence) arg6))) {
            if (this.f == null) {
                this.c(v3);
                if (this.f == null) {
                    this.f = new JSONObject();
                }
            }

            try {
                if (android.support.design.a.isTextEquals(this.f.optString(arg5, null), arg6)) {
                    return;
                }

                this.f.put(arg5, arg6);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }

            if (!this.g) {
                this.g = true;
                new Handler(Looper.getMainLooper()).postDelayed(new k(this), 1000);
            }

            if (!this.a) {
                return;
            }

            this.a();
        }
    }

    protected final void a(String arg5, String[] arg6) {
        if (arg6 != null) {
            String v0 = TextUtils.join("\n", ((Object[]) arg6));
            if (Logger.debug()) {
                Logger.d("DirectoryCacheHelper", "cacheStringArray key = " + arg5 + " value = " + v0 + " cacheDir = " + this.e);
            }

            this.a(arg5, v0);
        }
    }

    private String b() {
        return this.e + "/device_parameters.dat";
    }

    protected final String[] b(String arg5) {
        String v0 = this.c(arg5);
        if (Logger.debug()) {
            Logger.d("DirectoryCacheHelper", "getCachedStringArray key = " + arg5 + " value = " + v0 + " cacheDir = " + this.e);
        }

        String[] v0_1 = TextUtils.isEmpty(((CharSequence) v0)) ? null : v0.split("\n");
        return v0_1;
    }

    private String c() {
        char[] v0 = new char[21];
        String v1 = this.c.getString(com.ss.android.article.browser.R.string.nl) + "bytedance";
        v0[5] = v1.charAt(10);
        v0[11] = v1.charAt(13);
        v0[17] = v1.charAt(11);
        v0[2] = v1.charAt(1);
        v0[18] = v1.charAt(4);
        v0[10] = v1.charAt(12);
        v0[0] = v1.charAt(1);
        v0[6] = v1.charAt(8);
        v0[13] = v1.charAt(5);
        v0[8] = v1.charAt(9);
        v0[1] = v1.charAt(3);
        v0[12] = v1.charAt(13);
        v0[4] = v1.charAt(15);
        v0[14] = v1.charAt(18);
        v0[7] = v1.charAt(17);
        v0[16] = v1.charAt(14);
        v0[3] = v1.charAt(11);
        v0[15] = v1.charAt(13);
        v0[19] = v1.charAt(19);
        v0[9] = v1.charAt(9);
        v0[20] = v1.charAt(7);
        return new String(v0).substring(2, 18);
    }

    private String c(String arg8) {
        FileLock v3 = null;
        Throwable v0_1;
        RandomAccessFile v2 = null;
        int v6 = 2049;
        String v0 = null;
        if (!android.support.design.a.isTextEmpty(arg8) && this.f != null) {
            return this.f.optString(arg8, v0);
        }

        if (!"mounted".equals(this.b)) {
            return v0;
        }

        try {
            File v1_3 = new File(this.e);
            if (!v1_3.exists() && !v1_3.mkdirs()) {
                return v0;
            }

            v1_3 = new File(this.b());
            v2 = new RandomAccessFile(v1_3, "rwd");
            v3 = v2.getChannel().lock();
            if (v1_3.isFile()) {
                byte[] v1_4 = new byte[2049];
                int v4 = v2.read(v1_4, 0, 2049);
                if (v4 > 0 && v4 < v6) {
                    this.f = new JSONObject(this.a(v1_4));
                    if (android.support.design.a.isTextEmpty(arg8)) {
                        return null;
                    } else {
                        return this.f.optString(arg8, null);
                    }
                }
                this.f = new JSONObject();
                return v0;
            }
        } catch (JSONException v1_2) {
            v1_2.printStackTrace();
            this.f = new JSONObject();
        }catch (Exception v1_1) {
            v1_1.printStackTrace();
            Logger.d("DirectoryCacheHelper", "load openudid exception " + v1_1);
        } finally {
            if(v3 != null) {
                try {
                    v3.release();
                } catch (Exception v1_1) {
                }
            }

            if(v2 != null) {
                try {
                    v2.close();
                } catch (Exception v1_1) {
                }
            }
        }
        return v0;
    }

    private byte[] d(String arg6) {
        byte[] v0_4;
        if (DirectoryCacheHelper.i != null && (android.support.design.a.equal(arg6, DirectoryCacheHelper.i.first))) {
            v0_4 = (byte[]) DirectoryCacheHelper.i.second;
            return ((byte[]) v0_4);
        }

        if (DirectoryCacheHelper.h != null && (android.support.design.a.equal(arg6, DirectoryCacheHelper.h.second))) {
            v0_4 = (byte[]) DirectoryCacheHelper.h.first;
            return ((byte[]) v0_4);
        }

        byte[] v1 = null;
        try {
            String v0_2 = this.c();
            SecureRandom v2 = new SecureRandom();
            Key v0_3 = android.support.design.a.a(v0_2.getBytes());
            Cipher v3 = Cipher.getInstance("DES");
            v3.init(1, v0_3, v2);
            v0_4 = Base64.encode(v3.doFinal(arg6.getBytes("UTF-8")), 0);
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0_4 = v1;
        }

        if (v0_4 != null) {
            DirectoryCacheHelper.i = new Pair(arg6, v0_4);
        }

        return ((byte[]) v0_4);
    }
}

